| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c | Prints: BFCTAT | The right operand of the conditional "or" operator is evaluated only if the left hand operand is false. In this case, the left operand of the first conditional "or" operator is false so the right hand operand is evaluated. No further evaluation of the expression is necessary so the right hand operand of the second conditional "or" operator is not evaluated. |
| 2 | f g h | Compiler error at 6 Compiler error at 7 Compiler error at 8 | The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char. |
| 3 | f | Prints: true,false,true | Positive infinity is equal to positive infinity. NaN is not equal to anything including itself. |
| 4 | b | Prints: NaN,NaN,2.0 | If the dividend is infinity then the result is NaN. If the dividend is finite and the divisor is infinity, then the result is the dividend. |
| 5 | f | Runtime error at line 10. | The object referenced by obj is of type "Base[]", and can not be cast to type "Sub[]". |
| 6 | d | Prints: 2, 2, -3, 3, 4, | |
| 7 | d | Runtime error at line 10. | The compiler accepts the explicit cast at line 10. However, Base is not a subclass of Sub; therefore, a runtime exception is thrown. |
| 8 | e | Compiler error at line 6. | An explicit cast to type int is required. |
| 9 | a | Prints: true | A char type literal or variable can be cast to a numberic value. |
| 10 | d | Prints: BB | Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, B is more specific than A because B extends A. The method m(B b, B b) is more specific than the others because both parameters are the most specific. |
| 11 | c | Prints: 5 | A compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1. Therefore, the statements int a=1; a+=++a+a++; can be rewritten as a=(int)((1)+(++a + a++));. With further evaluation we have a=(int)((1)+(2 + 2)). |
| 12 | b | Prints: 2, 2, -3, -4, 12, | Integral primitives are stored in two's compliment format. To change a positive value into a negative value, invert each bit and add one to the result. The "~" operator is the bitwise compliment operator. It inverts the value of each bit. For example, ~0=-1. |
| 13 | i | Compiles and runs without error. | Although the reference named base is of type Base, the object that it refers to is of type Sub. Therefore, the object may be cast to type Sub. Since objects of type Sub implement both interfaces, I1 and I2, the Sub type objects can be assigned to references of type I1 and I2 without an explicit cast. |
| 14 | b | Prints: String | Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, the String type is more specific than the Object type because String extends Object. |
| 15 | g | Compiler error at line 6. | An array of primitive types can not be cast to an array of a different primitive type. |
| 16 | e | Compiler error at line 5. | The length attribute of the array is accessed as though it were a method. |
| 17 | c | Compiler error. | Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, both the String class and class A are more specific than type Object because both extend Object. However, String and A are equally specific because one does not extend the other. The ambiguity generates a compiler error. |
| 18 | g | None of the above. | The program compiles and runs without error and prints 112. It is necessary to remember that all arrays are objects and therefore are clonable. Furthermore, a two dimensional array is also a single dimensional array of single dimensional arrays. |